<?php
/**
 * 列表页控制器
 * @author e5
 *
 */
class ListController extends CommonController{
	private $db;
	public function __init(){
		parent::__init();
		$this->db = K("Category");
	}
	public function index(){
		//获得全部分类
		$cateData = $this->db->all();
		//获得顶级分类
		$topCate = array();
		foreach ($cateData as $v){
			if ($v['pid'] == 0){
				$topCate[] = $v;
			}
		}
		$this->assign("topCate",$topCate);
		$this->assign("cateData",$cateData);
		$cid = Q("get.cid",0,"intval");
		if ($cid == 0) go(U("Index/index"));
		if (empty($cateData)) go(U("Index/index"));
		//获得当前分类的所有子分类的cid
		$cids = $this->db->getSonCid($cid,$cateData);
		//将当前分类cid压入
		$cids[] = $cid;
		//获得当前分类和其子分类下的所有商品id
		$gids = K("Goods")->cidsGetGids($cids);
		if ($gids){
			//获得商品属性
			$attr = K("GoodsAttr")->gidsGetAttrs($gids);
			$this->assign("attr",$attr);
			//筛选参数***
			//统计参数个数(参数个数与$attr元素个数相等)
			$pTotal = count($attr);
			//参数存在就通过"_"拆分成数组(数组键值为商品属性id)，不存在就生成一个键值全部为0的数组
			$param = isset($_GET['param'])?explode("_", $_GET['param']):array_fill(0, $pTotal, 0);
			//如果用户手动删除或增加参数，将参数全部设置为0(0为不限)
			if (count($param) != $pTotal) $param = array_fill(0, $pTotal, 0);
			$this->assign("param",$param);
			//通过$param筛选商品id
			$newGids = K("GoodsAttr")->filerGids($param,$gids);
		}else {
			//如果分类下面没有商品
			$newGids = array();
		}
		//如果分类下面有商品
		$pageData = "";
		if ($newGids){
			//处理子分类商品个数*****
			//获得当前分类下一级分类数据
			$sonCids = $this->db->field("cid,cname")->where("pid={$cid} OR cid={$cid}")->all();
			foreach ($sonCids as $sck=>$scv){
				$tempCids = $this->db->getSonCid($scv['cid'],$cateData);
				$tempCids[] = $scv['cid'];
				$sonCids[$sck]['goodsnum'] = K("Goods")->where("vshop_category_cid IN (".implode(",", $tempCids).")")->count();
			}
			$this->assign("sonCids",$sonCids);
			//处理排序
			$order = Q("get.order",0,"intval");
			switch ($order){
				//0为热门降序
				case 0:
					$orderStr = "gclick DESC";
					break;
				//1为热门升序
				case 1:
					$orderStr = "gclick ASC";
					break;
				//2为销量降序
				case 2:
					$orderStr = "gclick DESC";
					break;
				//3为销量升序
				case 3:
					$orderStr = "gclick ASC";
					break;
				//4为好评降序
				case 4:
					$orderStr = "gclick DESC";
					break;
				//5为好评升序
				case 5:
					$orderStr = "gclick ASC";
					break;
				//6为添加时间降序
				case 6:
					$orderStr = "guptime DESC";
					break;
				//7为添加时间升序
				case 7:
					$orderStr = "guptime ASC";
					break;
				//8为价格降序
				case 8:
					$orderStr = "gshopprice DESC";
					break;
				//9为价格升序
				case 9:
					$orderStr = "gshopprice ASC";
					break;
				default:
					$orderStr = "gclick DESC";
					break;
			}
			$_GET['order'] = $order;
			//按价格查询
			$sPrice = "";
			if (IS_POST){
				$pstart = Q("post.pstart",0,"intval");
				$pend = Q("post.pend",0,"intval");
				if ($pend != 0 && $pend > $pstart){
					$sPrice .= " AND gshopprice>={$pstart} AND gshopprice<={$pend}";
				}
			}
			//处理分页
			$total = K("Goods")->where("gid IN (".implode(",", $newGids).")".$sPrice)->count();
			$page = new Page($total,15);
			if ($total > 15) $pageData = $page->show();
			//获得商品数据
			$goodsData = K("Goods")->field("gid,gname,gshopprice,gpic")->where("gid IN (".implode(",", $newGids).")".$sPrice)->order($orderStr)->limit($page->limit())->all();
		}else {
			//如果分类下面没有商品
			$goodsData = array();
			$total = 0;
		}
		//查询当前分类名称并向结果数组中压入商品数量
		$gNumNowCate = $this->db->field('cname')->where("cid={$cid}")->find();
		$gNumNowCate['gnum'] = $total;
		$this->assign("gNumNowCate",$gNumNowCate);
		$this->assign("pageData",$pageData);
		$this->assign("goodsData",$goodsData);
		//面包屑导航
		$fatherCateArr = $this->getFatherCate($cateData, $cid);
		//返转数组
		$fatherCateArr = array_reverse($fatherCateArr);
		$this->assign("fatherCateArr", $fatherCateArr);
		$this->dis("list");
	}
}